package com.ignou.mca.exam;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ignou.mca.beans.ExamBean;
import com.ignou.mca.beans.UserVO;
import com.ignou.mca.datasource.DatabaseOperations;

/**
 * Servlet implementation class Exam
 */
public class Exam extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Exam() 
    {
        super();
        // TODO Auto-generated constructor stub
    }
    
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		ServletContext context = getServletContext();
		HttpSession session = request.getSession();
		
		String commandIssued = request.getParameter("CMD");
		String examinationId = request.getParameter("examid");
		String examName = request.getParameter("examName");
		String noQues = request.getParameter("noOfQues");
		String questionIds=request.getParameter("questionIdsSelected");
		String mxMarks = request.getParameter("maxMarks");
		String passMark = request.getParameter("passMarks");
		String examFes = request.getParameter("examFees");
		String dur = request.getParameter("examDuration");
		String subId = request.getParameter("subjectID");
		
		int examId = Integer.parseInt(examinationId);
		int noOfQuestions = Integer.parseInt(noQues);
		int maxMarks = Integer.parseInt(mxMarks);
		int passMarks = Integer.parseInt(passMark);
		int examFees = Integer.parseInt(examFes);
		int duration = Integer.parseInt(dur);
		int subjectId = Integer.parseInt(subId);
		
		
		ExamBean eb = new ExamBean();
		eb.setExamId(examId);
		eb.setDuration(duration);
		eb.setExamFees(examFees);
		eb.setExamName(examName);
		eb.setMaxMarks(maxMarks);
		eb.setNoOfQuestions(noOfQuestions);
		eb.setPassMarks(passMarks);
		eb.setQuestionIds(questionIds);
		eb.setSubjectId(subjectId);
		
		DatabaseOperations dbOperations = new DatabaseOperations();
		UserVO uvo = (UserVO)session.getAttribute("UserVO");
		if(commandIssued.equalsIgnoreCase("update"))
		{
			Boolean isUpdated = dbOperations.updateExam(eb);
			if(isUpdated)
			{
				context.getRequestDispatcher("/Subjects.jsp").forward(request, response);
			}
			else
			{
				session.setAttribute("ErrorMessage", "Unable to to contact server at this time. <br>Please try again after sometime.");
				context.getRequestDispatcher("/ErrorDisplay.jsp").forward(request, response);
			}
		}
		else
		{
			Boolean isAdded = dbOperations.addExam(eb,uvo.getUserName());
			if(isAdded)
			{
				context.getRequestDispatcher("/Subjects.jsp").forward(request, response);
			}
			else
			{
				session.setAttribute("ErrorMessage", "Unable to to contact server at this time. <br>Please try again after sometime.");
				context.getRequestDispatcher("/ErrorDisplay.jsp").forward(request, response);
			}
		}
		
		
		
		
	}

}
